<template>
  <div :class="{ 'hidden': hidden }" class="pagination-container">
    <el-pagination
        :background="background"
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :layout="layout"
        :page-sizes="pageSizes"
        :pager-count="pagerCount"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
    />
  </div>
</template>

<script setup>
import { scrollTo } from '@/utils/scroll-to'

const props = defineProps({
  total: {
    required: true,
    type: Number
  },
  page: {
    type: Number,
    default: 1
  },
  limit: {
    type: Number,
    default: 20
  },
  pageSizes: {
    type: Array,
    default() {
      return [10, 20, 30, 50]
    }
  },
  // 移动端页码按钮的数量端默认值5
  pagerCount: {
    type: Number,
    default: document.body.clientWidth < 992 ? 5 : 7
  },
  layout: {
    type: String,
    default: 'total, sizes, prev, pager, next, jumper'
  },
  background: {
    type: Boolean,
    default: true
  },
  autoScroll: {
    type: Boolean,
    default: true
  },
  hidden: {
    type: Boolean,
    default: false
  }
})

const emit = defineEmits();
const currentPage = computed({
  get() {
    return props.page
  },
  set(val) {
    emit('update:page', val)
  }
})
const pageSize = computed({
  get() {
    return props.limit
  },
  set(val){
    emit('update:limit', val)
  }
})
function handleSizeChange(val) {
  if (currentPage.value * val > props.total) {
    currentPage.value = 1
  }
  emit('pagination', { page: currentPage.value, limit: val })
  if (props.autoScroll) {
    scrollTo(0, 800)
  }
}
function handleCurrentChange(val) {
  emit('pagination', { page: val, limit: pageSize.value })
  if (props.autoScroll) {
    scrollTo(0, 800)
  }
}

</script>

<style scoped>
.pagination-container {
  background: linear-gradient(145deg, rgba(255,255,255,0.9), rgba(246,248,250,0.9));
  padding: 24px 16px;
  border-radius: 12px;
  box-shadow: 0 4px 20px rgba(143,161,204,0.15);
  margin: 20px auto;
  max-width: 800px;
  transition: all 0.3s ease;
}

/* 深度选择器修改element组件样式 */
::v-deep .el-pagination {
  --el-color-primary: #5b8cff;
  --el-text-color-regular: #5c6b7a;
}

::v-deep .el-pagination button,
::v-deep .el-pager li {
  background: transparent;
  border: 1px solid rgba(228,235,242,0.8);
  border-radius: 8px;
  margin: 0 4px;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  color: #5c6b7a;
  min-width: 32px;
  height: 32px;
}

::v-deep .el-pagination button:hover,
::v-deep .el-pager li:hover {
  transform: translateY(-2px);
  box-shadow: 0 2px 8px rgba(91,140,255,0.2);
  background: rgba(236,245,255,0.6);
  color: #2c3e50;
}

::v-deep .el-pagination.is-background .el-pager li:not(.is-disabled).is-active {
  background: linear-gradient(135deg, #89b4ff, #5b8cff);
  color: white;
  font-weight: 500;
  box-shadow: 0 2px 6px rgba(91,140,255,0.3);
}

::v-deep .el-pagination__total {
  color: #7a8aa2;
  font-size: 14px;
}

::v-deep .el-pagination__jump {
  color: #7a8aa2;
}

::v-deep .el-input__inner {
  border-radius: 8px;
  border-color: rgba(228,235,242,0.8);
  transition: all 0.3s ease;
}

::v-deep .el-input__inner:focus {
  border-color: #5b8cff;
  box-shadow: 0 0 0 2px rgba(91,140,255,0.1);
}

/* 移动端适配 */
@media (max-width: 768px) {
  .pagination-container {
    padding: 16px;
    border-radius: 8px;
  }

  ::v-deep .el-pagination__jump {
    display: none;
  }

  ::v-deep .el-pagination button,
  ::v-deep .el-pager li {
    min-width: 28px;
    height: 28px;
    margin: 0 2px;
  }
}
</style>
